package com.teach.demo03;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

public class Client {

	public static void main(String[] args) {
		try {
			Socket client = new Socket("192.168.31.10", 8888);
			
			//向服务端发送文字
			OutputStream out1 = client.getOutputStream();
			DataOutputStream out = new DataOutputStream(out1);
			Scanner sc = new Scanner(System.in);
			
			new Thread(()->{
				String line = null;
				while(true){
					line = sc.next();
					if("exit".equalsIgnoreCase(line)){
						try {
							client.close();
						} catch (Exception e) {
							return;
						}
						return;
					}
					try {
						out.writeUTF(line);
					} catch (Exception e) {
						return;
					}
				}
				
			}).start();

			//接受服务器端发送的文字
			InputStream in1 = client.getInputStream();
			DataInputStream in = new DataInputStream(in1);
			new Thread(()->{
				while(true){
					try {
						System.out.println(in.readUTF());
					} catch (Exception e) {
						return;
					}
				}
			}).start();

		}catch (Exception e) {
			e.printStackTrace();
		}
	}

}
